﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Ewts.ashx
{
    /// <summary>
    /// Casc 的摘要说明
    /// </summary>
    public class Casc : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string str = "";
            
            if (context.Request["iotype"] != null)
            {
                string iotype = context.Request["iotype"];
                switch(iotype){
                    case "hard":
                        str = InsertHard(context).ToString();
                        break;
                    case "m":
                        str = GetHardModel(int.Parse(context.Request["mid"])).ToString();
                        break;
                    case "goback":  //退回上一步
                        str = CheckBack(int.Parse(context.Request["hardid"]));
                        break;
                    default:
                        break;

                }
            }
            context.Response.Write(str);
        }

        /// <summary>
        /// 新增送检
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public int InsertHard(HttpContext context)
        {
            DataLib.BLL.Hardware bll = new DataLib.BLL.Hardware();
            DataLib.Model.Hardware model = new DataLib.Model.Hardware();
            if (context.Request["hardid"] != null && context.Request["hardid"].Trim() != "" && context.Request["hardid"] != "0")
            {

                model = bll.GetModel(int.Parse(context.Request["hardid"]));
            }
            else
            {
                model.id = 0;
            }
            if (context.Request["txtcjo"] != null && context.Request["txtcjo"].Trim() != "")
            {
                model.cjono = context.Request["txtcjo"];
            }
            if (context.Request["txtload"] != null && context.Request["txtload"].Trim() != "")
            {
                model.loadno = context.Request["txtload"];
            }
            if (context.Request["txtcjoremark"] != null && context.Request["txtcjoremark"].Trim() != "")
            {
                model.cjoremark = context.Request["txtcjoremark"];
            }

            if (context.Request["txtcount"] != null && context.Request["txtcount"].Trim() != "")
            {
                model.boxcount = context.Request["txtcount"];
            }
            if (context.Request["txtcumname"] != null && context.Request["txtcumname"].Trim() != "")
            {
                model.cumname = context.Request["txtcumname"];
            }
            if (context.Request["txtcumcdoe"] != null && context.Request["txtcumcdoe"].Trim() != "")
            {
                model.cumcode = context.Request["txtcumcdoe"];
            }
            if (context.Request["txtman"] != null && context.Request["txtman"].Trim() != "")
            {
                model.saleman = context.Request["txtman"];
            }
            if (context.Request["txtwrldate"] != null && context.Request["txtwrldate"].Trim() != "")
            {
                model.wrldate = context.Request["txtwrldate"];
            }
            if (context.Request["bcity"] != null && context.Request["bcity"].Trim() != "")
            {
                model.bcity = context.Request["bcity"];
            } if (context.Request["gcity"] != null && context.Request["gcity"].Trim() != "")
            {
                model.gcity = context.Request["gcity"];
            }
            if (context.Request["checkdate"] != null && context.Request["checkdate"].Trim() != "")
            {
                model.inspectiondate = context.Request["checkdate"];
            }
            if (context.Request["initremark"] != null && context.Request["initremark"].Trim() != "")
            {
                model.initremark = context.Request["initremark"];
            }
            if (context.Request["initdate"] != null && context.Request["initdate"].Trim() != "")
            {
                model.initcheck = context.Request["initdate"];
            }
            if (context.Request["hardremark"] != null && context.Request["hardremark"].Trim() != "")
            {
                model.hardremark = context.Request["hardremark"];
            }
            if (context.Request["outtime"] != null && context.Request["outtime"].Trim() != "")
            {
                model.outstime = context.Request["outtime"];
            }
            if (context.Request["finshdate"] != null && context.Request["finshdate"].Trim() != "")
            {
                model.hardwarefinish = context.Request["finshdate"];
            }
            if (context.Request["tallyremark"] != null && context.Request["tallyremark"].Trim() != "")
            {
                model.tallyremark = context.Request["tallyremark"];
            }
            if (model.id != 0)
            {
                if (context.Request["step1"] != null && context.Request["step1"].Trim() != "")
                {
                    string c_old ="";
                    if (model.flag1 == null)
                    {
                        model.flag1 = "";
                    }
                    else
                    {
                        if (model.flag1.Length > 0)
                        {
                            c_old = model.flag1.Substring(model.flag1.Length - 1, 1);
                        }
                    }
                    model.step1 = int.Parse(context.Request["step1"].Trim());
                    switch (model.step1)
                    {
                        case 0:
                            if (c_old != "A")
                            {
                                model.flag1 = model.flag1 + "A";    //新增完成待送检
                            }
                            break;
                        case 1:
                            if (c_old != "B")
                            {
                                model.flag1 = model.flag1 + "B";    //送检完成待初检
                            }
                            break;
                        case 2:
                            if (c_old != "C")
                            {
                                model.flag1 = model.flag1 + "C";    //初检完成待硬件
                            }
                            break;
                        case 3:
                            if (c_old != "D")
                            {
                                model.flag1 = model.flag1 + "D";    //硬件完成待出库
                            }
                            break;
                        case 4:
                            if (c_old != "E")
                            {
                                model.flag1 = model.flag1 + "E";    //出库完成进入查询维护
                            }
                            break;
                        default:
                            break;
                    }
                }
                if (bll.Update(model) == true)
                    return 0;
                else
                    return -1;
            }
            else
            {
                model.flag1 = "A";
                return bll.Add(model);
            }

        }



        /// <summary>
        /// 取得对应的硬件送检记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetHardModel(int id)
        {
            DataLib.BLL.Hardware bll = new DataLib.BLL.Hardware();
            DataLib.Model.Hardware model = bll.GetModel(id);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(model);

        }

        /// <summary>
        /// 统一退回上一步  FLAG1 代表历史步骤  A(新增）B（送检）C（初检）D(硬件测试）E（出库)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string CheckBack(int id)
        {
            DataLib.BLL.Hardware bll = new DataLib.BLL.Hardware();
            DataLib.Model.Hardware model = bll.GetModel(id);
            
            if (model != null)
            {
               
                if (model.flag1.Length > 1)
                {
                    model.flag1 = model.flag1.Substring(0, model.flag1.Length - 1);
                }
                string step = model.flag1.Substring(model.flag1.Length - 1, 1);
                switch (step)
                {
                    case "A":
                        model.step1 = 0;    //待送检
                        break;
                    case "B":
                        model.step1 = 1;    //待初检
                        break;
                    case "C":
                        model.step1 = 2;    //待硬件测试
                        break;
                    case "D":
                        model.step1 = 3;    //待出库理货
                        break;
                    case "E":
                        model.step1 = 4;    //已经完成
                        break;
                    default:
                        break;
                }
              
            }

            return bll.Update(model).ToString();

        }

        /// <summary>
        /// 初检提交
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string InitCheckSubmit(int id,string checkdate)
        {
            DataLib.BLL.Hardware bll = new DataLib.BLL.Hardware();
            DataLib.Model.Hardware model = bll.GetModel(id);
            if (model != null)
            {
                model.inspectiondate = checkdate;
                model.step2 = model.step1;
                model.step1 = 2;
            }

            return bll.Update(model).ToString();

        }

     
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="finishdate"></param>
        /// <param name="initremark"></param>
        /// <param name="hardremark"></param>
        /// <returns></returns>
        public string HardSubmit(int id, string finishdate, string initremark, string hardremark)
        {
            DataLib.BLL.Hardware bll = new DataLib.BLL.Hardware();
            DataLib.Model.Hardware model = bll.GetModel(id);
            if (model != null)
            {
                model.step1 = 3;
                
                model.initremark = initremark;
                model.hardwarefinish = finishdate;
                model.tallyremark = hardremark;
            }

            return bll.Update(model).ToString();
        }

     
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}